package com.saitan.demo.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import java.util.ArrayList;

public class PermissionUtils {

    public static int RequestCode = 1;

    public static void initPermission(Context context, Activity activity) {
        String permissions[] = {
                Manifest.permission.READ_PHONE_NUMBERS,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.INTERNET,
                Manifest.permission.CHANGE_WIFI_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.CAMERA,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.VIBRATE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,
                Manifest.permission.BLUETOOTH,
                Manifest.permission.BLUETOOTH_ADMIN,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.REQUEST_INSTALL_PACKAGES,
                Manifest.permission.SYSTEM_ALERT_WINDOW,
        };

        //所有未获取的权限
        ArrayList<String> notAllowList = new ArrayList<String>();
        for (String perm : permissions) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(context, perm)) {
                //进入到这里代表没有权限.
                notAllowList.add(perm);
            }
        }

        //重新申请权限
        String tmpList[] = new String[notAllowList.size()];
        if (!notAllowList.isEmpty()) {
            ActivityCompat.requestPermissions(activity, notAllowList.toArray(tmpList), RequestCode);
        }
    }

}
